package distributedbank.server.messages;

import java.io.IOException;

import distributedbank.Wrapper;
import distributedbank.server.BankBranch;

/**
 * A transaction is a message Request relating to the operations of the bank.
 * Sub-classes are: Query, Deposit, Transfer and TransferDeposit. Snapshot
 * requests, however, are not sub-classes of class Transaction.
 */
public abstract class Transaction
        extends Request {
    private static final long serialVersionUID = 1;

    public final int transactionId;

    protected final int accountId; // account ID without the digits for branchId

    public final int fromPort;

    public Transaction(int branchId, int accountId, int transactionId,
            int fromPort) {
        super(branchId);
        this.accountId = accountId;
        this.transactionId = transactionId;
        this.fromPort = fromPort;
    }

    @Override
    public void runRequest(BankBranch bankBranch, Wrapper wrapper)
            throws IOException, ClassNotFoundException {
        wrapper.sendSyncRequests(this);

        Acknowledgment message = this.runRequestSilent(bankBranch);
        message.fromPort = wrapper.localPort;
        wrapper.sendAcknowledgment(message, this.fromPort);
    }

    /**
     * Runs the request to affect local state, but doesn't send any
     * acknowledgment/transfer messages.
     */
    public abstract Acknowledgment runRequestSilent(BankBranch bankBranch);

}
